import deepClone from './deepClone'
import deepMerge from "./deepMerge";
import VueCookies from "vue-cookies";

/**
 * 格式化日期时间
 * @param times 时间字段
 * @param format 格式规则
 * @return {string}
 */
export function getFormat(times, format='YYYY-MM-DD HH:mm:ss') {
    if (times === null) return ''
    // 获取对应时间的格式
    times = new Date(times)
    const y = times.getFullYear(),
        m = times.getMonth() + 1 < 10 ? '0' + (times.getMonth() + 1) : times.getMonth() + 1,
        d = times.getDate() < 10 ? '0' + times.getDate() : times.getDate(),
        h = times.getHours() < 10 ? '0' + times.getHours() : times.getHours(),
        mm = times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes(),
        ss = times.getSeconds() < 10 ? '0' + times.getSeconds() : times.getSeconds();
    switch (format) {
        case'YYYY-MM-DD':
            return y + '-' + m + '-' + d
        case'MM-DD':
            return  m + '-' + d
        case 'YYYY-MM-DD HH:mm:ss':
            return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + ss
        case 'YYYY-MM-DD HH:mm':
            return y + '-' + m + '-' + d + ' ' + h + ':' + mm
        case 'HH:mm:ss':
            return h + ':' + mm + ':' + ss
        case 'HH:mm':
            return h + ':' + mm
        default:
            break
    }
}




/**
 * 获取cookie
 * @param key
 * @return {*|string}
 */
export const getCookies = (key='token') => {
    return VueCookies.get(key) || '';
}
/**
 * 清除cookie
 * @param key
 */
export const removeCookies = (key='token') => {
    VueCookies.remove(key);
}
/**
 * 设置cookie
 * @param key
 * @param value
 */
export const setCookies = (key='token', value='') => {
    VueCookies.set(key,value);
}
/**
 * 获取uuid
 */
export const getUUID = () => {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
    })
}

// 数字千分位逗号分隔
export const thousands = (num) => {
    // num += Math.random() * 101321235
    try {
        if (String(num).indexOf(".") + 1 > 0) {
            num = parseFloat(num)
            return parseFloat(num.toFixed(2)).toLocaleString()
        } else {
            return num.toFixed(2).toLocaleString()
        }
    } catch (e) {
        // console.log(e)
        return num
    }
}

export const camelToSnake = (camelCaseString) => {
    return camelCaseString.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
}

export default {
    deepClone,
    deepMerge
}
